package com.example.weddingapp.activity;

import android.graphics.Bitmap;
import android.graphics.Typeface;
import android.os.Bundle;
import android.view.View;

import androidx.lifecycle.AndroidViewModel;

import com.example.weddingapp.R;
import com.example.weddingapp.base.BaseActivity;
import com.example.weddingapp.base.BaseObserver;
import com.example.weddingapp.databinding.ActivityAddBinding;
import com.example.weddingapp.utils.ImageLoaderUtil;
import com.example.weddingapp.utils.PinYinUtils;
import com.example.weddingapp.utils.ViewShootUtil;

import java.io.FileNotFoundException;
import java.util.Date;

import cn.leancloud.AVFile;
import cn.leancloud.AVObject;
import cn.leancloud.AVUser;

public class AddActivity extends BaseActivity<AndroidViewModel, ActivityAddBinding> {
    private String address;
    private String time;
    private String boy;
    private String girl;
    private Date date;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_add);

        showContentView();
        setTitle("");
        showRightButton(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                ViewShootUtil.viewShot(bindingView.llRoot, null, new ViewShootUtil.ShotCallback() {
                    @Override
                    public void onShotComplete(Bitmap bitmap, String savePath) {
                        submit(savePath);
                    }
                });
            }
        });

    }

    @Override
    protected void initView() {
        super.initView();

        ImageLoaderUtil.displayNet(this, "https://qnm.hunliji.com/o_1e63i4t9at3j2vbpaj7gs16sn36.png", bindingView.ivBg);
    }

    @Override
    protected void initData() {
        super.initData();
        address = getIntent().getStringExtra("address");
        time = getIntent().getStringExtra("time");
        boy = getIntent().getStringExtra("groomName");
        girl = getIntent().getStringExtra("brideName");
        date = (Date) getIntent().getSerializableExtra("timeDate");
        bindingView.tvTime.setText(time);
        bindingView.tvAddress.setText(address);
        bindingView.tvBoyName.setText(boy);
        bindingView.tvGirlName.setText(girl);

        Typeface tfLight = Typeface.createFromAsset(this.getAssets(), "BeastOfAvalon-OKmp.ttf");
        bindingView.etEnBoy.setTypeface(tfLight);

        Typeface tfLight2 = Typeface.createFromAsset(this.getAssets(), "WindyRainDemo-nao1.ttf");
        bindingView.etEnGirl.setTypeface(tfLight2);
        bindingView.tvAnd.setTypeface(tfLight2);

        try {
            String pingYinBoy = PinYinUtils.getPingYin(boy);
            String pingYinGirl = PinYinUtils.getPingYin(girl);
            bindingView.etEnBoy.setText(pingYinBoy);
            bindingView.etEnGirl.setText(pingYinGirl);
        } catch (Exception e) {

        }
    }


    private void submit(String photoPath) {
        AVFile file = null;
        try {
            file = AVFile.withAbsoluteLocalPath(System.currentTimeMillis() + ".png", photoPath);

        } catch (FileNotFoundException e) {
            e.printStackTrace();
        }

        String enBoy = bindingView.etEnBoy.getText().toString();
        String enGirl = bindingView.etEnGirl.getText().toString();

        AVObject object = new AVObject("Activity");
        object.put("user", AVUser.getCurrentUser());
        object.put("address", address);
        object.put("boy", boy);
        object.put("girl", girl);
        object.put("enBoy", enBoy);
        object.put("enGirl", enGirl);
        object.put("time", time);
        object.put("timeDate",date);
        object.put("image", file);
        object.saveInBackground().subscribe(new BaseObserver<AVObject>(this) {
            @Override
            public void onNext(AVObject avObject) {
                showToast("保存成功");
                finish();
            }

        });

    }

}
